package com.qk.controller;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public Result page(UserDto userDto) {
        log.info("userDto:{}", userDto);
        PageResult<User> pageResult = userService.selectPage(userDto);
        return Result.success(pageResult);
    }


    @PostMapping
    public Result save(@RequestBody User user) {
        log.info("userDto:{}", user);
        userService.save(user);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") List<Integer> ids) {
        log.info("ids:{}", ids);
        userService.delectByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("id:{}", id);
        User user = userService.selectById(id);
        return Result.success(user);
    }

    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("userDto:{}", user);
        userService.update(user);
        return Result.success();
    }

    @GetMapping("list")
    public Result list() {
        log.info("list");
        List<User> userList = userService.list();
        return Result.success(userList);
    }

    @GetMapping("role/{roleLabel}")
    public Result role(@PathVariable String roleLabel) {
        log.info("roleLabel:{}", roleLabel);
        List<User> userList = userService.listOfLabel(roleLabel);
        return Result.success(userList);
    }

    @GetMapping("dept/{deptId}")
    public Result dept(@PathVariable Integer deptId) {
        log.info("deptId:{}", deptId);
        List<User> userList = userService.listOfDept(deptId);
        return Result.success(userList);
    }


}
